package com.RedRiver.hospitalsystem.personal_service.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.RedRiver.hospitalsystem.personal_service.common.Personal_serviceResultCode;
import com.RedRiver.hospitalsystem.personal_service.common.exception.DoctorException;
import com.RedRiver.hospitalsystem.personal_service.infra.mapper.DoctorMapper;
import com.RedRiver.hospitalsystem.personal_service.infra.model.Account;
import com.RedRiver.hospitalsystem.personal_service.infra.model.Doctor;
import com.RedRiver.hospitalsystem.personal_service.service.DoctorService;
import com.RedRiver.hospitalsystem.personal_service.service.Form.*;
import com.RedRiver.hospitalsystem.personal_service.service.info.AccountPageInfo;
import com.RedRiver.hospitalsystem.personal_service.service.info.DoctorPageInfo;
import com.RedRiver.hospitalsystem.result.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.print.Doc;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 龙世超
 * @since 2022-04-26
 */
@Service
public class DoctorServiceImpl extends ServiceImpl<DoctorMapper, Doctor> implements DoctorService {

    @Resource
    private DoctorMapper doctorMapper;


    /**
     * 新增医生信息
     * @param doctorForm
     * @return
     */
    @Override
    public Result<?> createDoctor(DoctorForm doctorForm) {
        Doctor doctor = doctorMapper.selectOne(new QueryWrapper<Doctor>().eq("doctor_card", doctorForm.getDoctorCard()));
        if (doctor!=null)throw new DoctorException(Personal_serviceResultCode.DOCTOR_CARD_EXISTED);
        int insert = doctorMapper.insert(BeanUtil.copyProperties(doctorForm, Doctor.class));
        if (insert==0)throw new DoctorException(Personal_serviceResultCode.DOCTOR_ADDITION_FAILD);
        return Result.getInstance().setMsg("新增成功");
    }

    /**
     * 分页查询所有医生信息
     * @param form
     * @return
     */
    @Override
    public IPage<DoctorPageInfo> findPageAllDocktor(PageForm form) {
        Page<Doctor> doctorInfoPage = new Page<>();
        doctorInfoPage = this.baseMapper.selectPage(new Page<>(form.getPage() == 0 ? 1 : form.getPage(), form.getPageSize() == 0 ? 2 : form.getPageSize()),null);
        List<Doctor> doctorInfoList = doctorInfoPage.getRecords();
        List<DoctorPageInfo> doctorPageInfoList = BeanUtil.copyToList(doctorInfoList, DoctorPageInfo.class);
        Page<DoctorPageInfo> page = new Page<>();
        page.setRecords(doctorPageInfoList);
        page.setCurrent(doctorInfoPage.getCurrent());
        page.setSize(doctorInfoPage.getSize());
        page.setTotal(doctorInfoPage.getTotal());
        return page;
    }

    /**
     * 修改医生信息
     * @param
     * @return
     */
    @Override
    public Result<String> changeDoctor(DoctorChangeForm doctorChangeForm) {
        List<Doctor> doctor_card = doctorMapper.selectList(new QueryWrapper<Doctor>().eq("doctor_card", doctorChangeForm.getDoctorCard()));
        if (doctor_card.size()>1)
            throw new DoctorException(Personal_serviceResultCode.DOCTOR_CARD_REPEAT);
        for (Doctor doctor : doctor_card) {
            if (doctor.getDoctorId()!=doctorChangeForm.getDoctorId())throw new DoctorException(Personal_serviceResultCode.DOCTOR_CARD_REPEAT);
        }
        Doctor doctor1 = BeanUtil.copyProperties(doctorChangeForm, Doctor.class);
        int i = doctorMapper.update(doctor1, new QueryWrapper<Doctor>().eq("doctor_id", doctor1.getDoctorId()));
        if (i==0) throw new DoctorException(Personal_serviceResultCode.DOCTOR_CHANGE_ERROR);
        return Result.getInstance(String.class).setMsg("修改信息成功").setData(doctor1.getDoctorName());
    }

    /**
     * 分页查询部门下所有医生
     * @param doctorDepartmentForm
     * @return
     */
    @Override
    public IPage<DoctorPageInfo> findPageDepartmentDocktor(DoctorDepartmentForm doctorDepartmentForm) {
        Page<Doctor> doctorInfoPage = new Page<>();
        doctorInfoPage = this.baseMapper.selectPage(new Page<>(doctorDepartmentForm.getPage() == 0 ? 1 : doctorDepartmentForm.getPage(), doctorDepartmentForm.getPageSize() == 0 ? 2 : doctorDepartmentForm.getPageSize()),
                new QueryWrapper<Doctor>().eq("department_id",doctorDepartmentForm.getDepartmentId()));
        List<Doctor> doctorInfoList = doctorInfoPage.getRecords();
        List<DoctorPageInfo> doctorPageInfoList = BeanUtil.copyToList(doctorInfoList, DoctorPageInfo.class);
        Page<DoctorPageInfo> page = new Page<>();
        page.setRecords(doctorPageInfoList);
        page.setCurrent(doctorInfoPage.getCurrent());
        page.setSize(doctorInfoPage.getSize());
        page.setTotal(doctorInfoPage.getTotal());
        return page;
    }

    /**
     * 分页查询某一级别的所有医生
     * @param doctorLevelForm
     * @return
     */
    @Override
    public IPage<DoctorPageInfo> FindPageLevelDoctor(DoctorLevelForm doctorLevelForm) {
        Page<Doctor> doctorInfoPage = new Page<>();
        doctorInfoPage = this.baseMapper.selectPage(new Page<>(doctorLevelForm.getPage() == 0 ? 1 : doctorLevelForm.getPage(), doctorLevelForm.getPageSize() == 0 ? 2 : doctorLevelForm.getPageSize()),
                new QueryWrapper<Doctor>().eq("doctort_level",doctorLevelForm.getDoctortLevel()));
        List<Doctor> doctorInfoList = doctorInfoPage.getRecords();
        List<DoctorPageInfo> doctorPageInfoList = BeanUtil.copyToList(doctorInfoList, DoctorPageInfo.class);
        Page<DoctorPageInfo> page = new Page<>();
        page.setRecords(doctorPageInfoList);
        page.setCurrent(doctorInfoPage.getCurrent());
        page.setSize(doctorInfoPage.getSize());
        page.setTotal(doctorInfoPage.getTotal());
        return page;
    }


    /**
     * 根据id删除医生信息
     * @param doctorId
     * @return
     */
    @Override
    public Result<?> delDoctor(Integer doctorId) {
        int doctor_id = doctorMapper.delete(new QueryWrapper<Doctor>().eq("doctor_id", doctorId));
        if (doctor_id==0) throw new DoctorException(Personal_serviceResultCode.DOCTOR_DELETE_ERROR);
        return Result.getInstance().setMsg("医生信息删除成功");
    }


}
